/**
 * 
 */
package org.tigris.apexam.implementation;

import java.util.Map;

import org.tigris.apexam.Message;
import org.tigris.apexam.MessageFactory;
import org.tigris.apexam.configuration.ConfigurationManager;
import org.tigris.apexam.configuration.implementation.SimpleConfigurationManager;
import org.tigris.elfin.*;

/**
 * @author zhisen.huang
 * 
 */
public class SimpleMessageFactory implements MessageFactory {

	protected Map<String, Message> messages = null;

	protected ExpressionProcessor expressionProcessor = null;

	public SimpleMessageFactory() throws Exception {
		this("./test/resources/messges.properties");
	}

	public SimpleMessageFactory(String fileName) throws Exception {
		ConfigurationManager configurationManager = new SimpleConfigurationManager(
				fileName);
		this.messages = configurationManager.getMessages();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tigris.apexam.MessageFactory#getMessage(java.lang.String,
	 *      java.util.Map)
	 */
	public String getMessage(String messageName, Map<String, Object> params)
			throws Exception {
		Message message = messages.get(messageName);
		message.setExpressionProcessor(expressionProcessor);

		return message.getMessage(params);
	}

	/**
	 * @param expressionProcessor
	 *            the expressionProcessor to set
	 */
	public void setExpressionProcessor(ExpressionProcessor expressionProcessor) {
		this.expressionProcessor = expressionProcessor;
	}

	/**
	 * @param messages
	 *            the messages to set
	 */
	public void setMessages(Map<String, Message> messages) {
		this.messages = messages;
	}

}
